package com.portico.security

import grails.plugins.springsecurity.Secured

@grails.plugins.springsecurity.Secured(['ROLE_ADMIN_USER, ROLE_SUPER_USER'])
class UserAuthorityController {

  static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

  static scaffold = true

  def index = {
    redirect(action: "list", params: params)
  }

  def list = {
    params.max = Math.min(params.max ? params.int('max') : 10, 100)
    [userAuthorityInstanceList: UserAuthority.list(params), userAuthorityInstanceTotal: UserAuthority.count()]
  }

  def create = {
    def userAuthorityInstance = new UserAuthority()
    userAuthorityInstance.properties = params
    return [userAuthorityInstance: userAuthorityInstance]
  }

  def save = {
    def userAuthorityInstance = new UserAuthority(params)
    if (userAuthorityInstance.save(flush: true)) {
      flash.message = "${message(code: 'default.created.message', args: [message(code: 'userAuthority.label', default: 'UserAuthority'), userAuthorityInstance.id])}"
      redirect(action: "show", id: userAuthorityInstance.id)
    }
    else {
      render(view: "create", model: [userAuthorityInstance: userAuthorityInstance])
    }
  }

  def show = {
    def userAuthorityInstance = UserAuthority.get(params.id)
    if (!userAuthorityInstance) {
      flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'userAuthority.label', default: 'UserAuthority'), params.id])}"
      redirect(action: "list")
    }
    else {
      [userAuthorityInstance: userAuthorityInstance]
    }
  }

  def edit = {
    def userAuthorityInstance = UserAuthority.get(params.id)
    if (!userAuthorityInstance) {
      flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'userAuthority.label', default: 'UserAuthority'), params.id])}"
      redirect(action: "list")
    }
    else {
      return [userAuthorityInstance: userAuthorityInstance]
    }
  }

  def update = {
    def userAuthorityInstance = UserAuthority.get(params.id)
    if (userAuthorityInstance) {
      if (params.version) {
        def version = params.version.toLong()
        if (userAuthorityInstance.version > version) {

          userAuthorityInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'userAuthority.label', default: 'UserAuthority')] as Object[], "Another user has updated this UserAuthority while you were editing")
          render(view: "edit", model: [userAuthorityInstance: userAuthorityInstance])
          return
        }
      }
      userAuthorityInstance.properties = params
      if (!userAuthorityInstance.hasErrors() && userAuthorityInstance.save(flush: true)) {
        flash.message = "${message(code: 'default.updated.message', args: [message(code: 'userAuthority.label', default: 'UserAuthority'), userAuthorityInstance.id])}"
        redirect(action: "show", id: userAuthorityInstance.id)
      }
      else {
        render(view: "edit", model: [userAuthorityInstance: userAuthorityInstance])
      }
    }
    else {
      flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'userAuthority.label', default: 'UserAuthority'), params.id])}"
      redirect(action: "list")
    }
  }

  def delete = {
    def userAuthorityInstance = UserAuthority.get(params.id)
    if (userAuthorityInstance) {
      try {
        userAuthorityInstance.delete(flush: true)
        flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'userAuthority.label', default: 'UserAuthority'), params.id])}"
        redirect(action: "list")
      }
      catch (org.springframework.dao.DataIntegrityViolationException e) {
        flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'userAuthority.label', default: 'UserAuthority'), params.id])}"
        redirect(action: "show", id: params.id)
      }
    }
    else {
      flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'userAuthority.label', default: 'UserAuthority'), params.id])}"
      redirect(action: "list")
    }
  }
}
